package io.r2mo.test.common;

import io.r2mo.spi.SPI;
import io.r2mo.spring.junit5.AppBaseTestSupport;
import io.r2mo.test.common.domain.ProductCatEntity;
import io.r2mo.typed.json.JObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lang : 2025-09-07
 */
@SpringBootTest(classes = TestCommonApplication.class)
@TestPropertySource("classpath:env-test.properties")
@Transactional
@Component
@Slf4j
public class DbCrudHandlerTestCase extends AppBaseTestSupport<ProductCatEntity> {

    private ProductCatEntity executeInsert(final String filename) {
        final ProductCatEntity productCatEntity =
            this.inOne(filename, ProductCatEntity.class);
        return this.db().create(productCatEntity);
    }

    @Test
    @Commit
    public void insertProduct() {
        final long countBefore = this.db().count().orElse(0L);
        final ProductCatEntity inserted = this.executeInsert("HFS/database/wait-handler-insert.json");
        final ProductCatEntity queried = this.db().findOne(inserted.getId()).orElse(null);
        Assertions.assertNotNull(queried);
        log.info("[ R2MO ] 打印 cMetadata(String): {}", queried.getCMetadata());
        final ProductCatEntity removed = this.db().remove(inserted);
        Assertions.assertEquals(inserted, removed);
        final long countAfter = this.db().count().orElse(0L);
        Assertions.assertEquals(countBefore, countAfter);
    }

    @Test
    @Commit
    public void insert1Product() {
        final long countBefore = this.db().count().orElse(0L);
        final ProductCatEntity inserted = this.executeInsert("HFS/database/wait-handler-insert1.json");
        final ProductCatEntity queried = this.db().findOne(inserted.getId()).orElse(null);
        Assertions.assertNotNull(queried);
        log.info("[ R2MO ] 打印 cMetadata(JObject): {}", queried.getCMetadata());
        final ProductCatEntity removed = this.db().remove(inserted);
        Assertions.assertEquals(inserted, removed);
        final long countAfter = this.db().count().orElse(0L);
        Assertions.assertEquals(countBefore, countAfter);
    }

    @Test
    @Commit
    public void testSerialize() {
        final ProductCatEntity dser1 = this.inOne("HFS/database/wait-handler-dser1.json", ProductCatEntity.class);
        System.out.println(dser1.getCMetadata().encodePretty());

        final JObject ser1 = SPI.V_UTIL.serializeJson(dser1);
        System.out.println(ser1.encodePretty());
        final ProductCatEntity dser2 = this.inOne("HFS/database/wait-handler-dser2.json", ProductCatEntity.class);
        System.out.println(dser2.getCMetadata().encodePretty());
        final JObject ser2 = SPI.V_UTIL.serializeJson(dser2);
        System.out.println(ser2.encodePretty());

        final ProductCatEntity dser3 = this.inOne("HFS/database/wait-handler-dser3.json", ProductCatEntity.class);
        System.out.println(dser3.getCMetadata());
        final JObject ser3 = SPI.V_UTIL.serializeJson(dser3);
        System.out.println(ser3.encodePretty());
    }
}
